package day06;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class FileDemo {
    public static void main(String[] args) {
        System.out.println(getLinesFromFile("main/Main.java"));
        int fileNumbers = countAllFiles("main");
        int linesDir = countAllLinesFromDir("main");
        System.out.println(fileNumbers);

        System.out.println(linesDir);
    }

    private static int countAllFiles(String path) {
        File root = new File(path);
        if (!root.exists()) return 0;
        if (!root.isDirectory()) return 1;
        String[] list = root.list();
        System.err.println(Arrays.asList(list));
        int tot = 0;
        for (String sub : list) {
            File subFile = new File(path, sub);//!!!!!
            tot += countAllFiles(subFile.getAbsolutePath());
        }

        return tot;
    }

    private static int countAllLinesFromDir(String path) {
        File root = new File(path);
        if (!root.exists()) return 0;
        if (!root.isDirectory()) return getLinesFromFile(root.getAbsolutePath());
        String[] list = root.list();
        System.err.println(Arrays.asList(list));
        int tot = 0;
        for (String sub : list) {
            File subFile = new File(path, sub);//!!!!
            tot += countAllLinesFromDir(subFile.getAbsolutePath());
        }

        return tot;
    }

    static int getLinesFromFile(String path) {
        int lines = 0;
        try {
            Scanner cin = new Scanner(new File(path));
            while (cin.hasNextLine()) {
                ++lines;
                cin.nextLine();
            }
        } catch (FileNotFoundException e) {

        }


        return lines;

    }
}
